﻿
/// <reference path="Component.js" />
/// <reference path="_namespace.js" />

/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
Class: ajax.components.DoubleCombo
/*****************************************************************************/

ajax.lang.addNamespace("ajax.components");

//-----------------------------------------------------------------------------
ajax.components.DoubleCombo = function(client, doc, masterId, slaveId, url, options) // Constructor
{
    ajax.components.DoubleCombo.superConstructor.call(this, doc);
    
    this.client     = client;
    this.master     = doc.getElementById(masterId);
    this.slave      = doc.getElementById(slaveId);
    this.options    = options;
    
    // client, onload, onerror, url, method, requestParams. 
    // Only pass our Error handler if our client gave us one
    this.ajaxHelper = new ajax.net.ContentLoader(this, 
        this.ajaxUpdate, 
        (this.options.errorHandler)? this.ajaxError : null, 
        url, "POST", options.requestParameters || []);
    
    this.initializeBehaviour();
}

//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.components.DoubleCombo", "ajax.components.Component"); // Inherit

//-----------------------------------------------------------------------------
ajax.components.DoubleCombo.prototype = 
{
    initializeBehaviour: function()
    {
        var oThis = this;
        this.master.onchange = function() { oThis.masterOnChange(); };
    },
    
    masterOnChange: function()
    {
        var query = "";
        for (var i = 0; i < this.master.options.length; i++)
        {
            if (this.master.options[i].selected)
            {
                if (query != "")
                    query += "&";
                query += "q=" + this.master.options[i].value;
            }
        }
        this.ajaxHelper.sendRequest(query);
    },
    
    ajaxUpdate: function(request)
    {
        // called in this context
        var slaveOptions    = this.createOptions(request.responseXML.documentElement);
        this.slave.length   = 0;
        for (var i = 0; i < slaveOptions.length; i++)
        {
            try
            {
                this.slave.options.add(slaveOptions[i], null);
            }
            catch(e)
            {
                this.slave.options.add(slaveOptions[i], -1);
            }
        }
    },
    
    ajaxError: function(request)
    {
        // if the client sent us an error handler, call it with their context
        if (this.options.errorHandler)
            this.options.errorHandler.call(this.client, request);
    },
    
    createOptions: function(ajaxResponse)
    {
        var newOptions = [];
        var entries = ajaxResponse.getElementsByTagName("entry");

        // Put in the default row
        if (entries.length > 0 && this.options.clientOptions)
            for (var i = 0; i < this.options.clientOptions.length; i++)
                newOptions.push(this.options.clientOptions[i]);
        // Add in item rows
        for (var i = 0; i < entries.length; i++)
        {
            var text     = this.getElementContent(entries[i], "optionText");
            var value    = this.getElementContent(entries[i], "optionValue");
            newOptions.push(new Option(text, value));
        }
        return newOptions;
    },
    
    getElementContent: function(element, tagName)
    {
        var childElement = element.getElementsByTagName(tagName)[0];
        return (childElement.text != undefined) ? childElement.text : childElement.textContent;
    }
}

ajax.components.DoubleCombo.prototype.toString = function()
{
    return "ajax.components.DoubleCombo";
}
ajax.components.DoubleCombo.prototype.toClassName = function()
{
    return ajax.lang.getClassName("ajax.components.DoubleCombo");
}


//-----------------------------------------------------------------------------
ajax.components.DoubleCombo.cast = function(obj)
{///<returns type="ajax.components.DoubleCombo"/>
    return obj;
}